var mongoose = require('mongoose');
mongoose.connect('mongodb://120.26.134.103:4000/parse');

var Schema = mongoose.Schema;
// 定义模型
var Order = mongoose.model('OPOrder', new Schema({_created_at: Date, _id: String, orderProducts:Array, status: Number, _updated_at:Date, checkArrived:String}), 'OPOrder');
var OrderProducts = mongoose.model('OPOrderProduct', new Schema({_id: String}), 'OPOrderProduct');
var currentDate = new Date();
// 检查是否有超过1天没付款的
function checkConfirm() {
    // 查找status等于0的order
    var gtDate = new Date();
    var ltDate = new Date();
    var query = Order.find({status: 0, _created_at:{$gt: gtDate.setDate(currentDate.getDate() - 2), $lt: ltDate.setDate(currentDate.getDate() - 1)}}).select({_created_at: 1, _id: 1, orderProducts: 1});
    var promise = query.exec();
    promise.addBack(function (err, docs) {
        if (err){
            console.log(err);
        }else {

            // var data = [];
            // docs.map(function (item) {
            //     var interval = ((currentDate.getTime()-item._created_at)/(1000*60*60*24)).toFixed(1);
            //     if (interval>1){
            //         data.push(item);
            //     }
            //
            // });
            if (docs.length!=0){
                deleteOrder(data);
            }else {
                console.log('未发现超过半小时未付款的数据');
            }
        }
    });
}
// 检查是否有7天未确认收货的
function checkArrived() {
    // 查找status是2的order
    var gtDate = new Date();
    var ltDate = new Date();
    var query = Order.find({status: 2, _updated_at:{$gt: gtDate.setDate(currentDate.getDate() - 8), $lt: ltDate.setDate(currentDate.getDate() - 7)}}).select({_updated_at:1, _id:1});
    var promise = query.exec();
    promise.addBack(function (err, docs) {
        if (err){
            console.log(err);
        }else {
            // var orders = [];
            // docs.map(function (item) {
            //     //计算时间间隔
            //     var interval = ((currentDate.getTime()-item._updated_at)/(1000*60*60*24)).toFixed(1);
            //     if (interval>7){
            //         console.log('发现超过7天未确认收货');
            //         orders.push(item);
            //     }
            //
            // });
            if (docs.length!=0){
                confirmArrived(orders);
            }else {
                console.log('未发现超过7天未确认收货的数据');
            }
        }
    });
}
// 删除订单
function deleteOrder(orders) {
    var orderIds = [];
    var orderProductIds = [];
    orders.map(function (order) {
        orderIds.push(order._id);
        order.orderProducts.map(function (orderProduct) {
            orderProductIds.push(orderProduct.objectId);
        });
    });
    deleteOderProduct(orderProductIds);
    var query = Order.remove({_id:{$in: orderIds}});
    var promise = query.exec();
    promise.addBack(function (err, docs) {
        if (err){
            console.log(err);
        }else {
            console.log('delete  ' + docs.result.n + '  order item');
        }
    });
}
// 删除orderproduct
function deleteOderProduct(orderProductIds) {
    var query = OrderProducts.remove({_id:{$in: orderProductIds}});
    var promise = query.exec();
    promise.addBack(function (err, docs) {
        if (err){
            console.log(err);
        }else {
            console.log('delete  ' + docs.result.n + '  orderProduct item');
        }
    });
}
// 自动确认订单
function confirmArrived(orders) {
    var orderIds = [];
    orders.map(function (order) {
        orderIds.push(order._id);
    });
    var query = Order.update({_id:{$in: orderIds}}, {status: 3, checkArrived:"auto"}, { multi: true });
    var promise = query.exec();
    promise.addBack(function (err, docs) {
        if (err){
            console.log(err);
        }else {
            console.log('update  ' + docs.n + '  order item');
        }
    });
}
// 执行检查
checkConfirm();
checkArrived();
